package com.league.zjxn.jetpack.repository

import com.blankj.utilcode.util.GsonUtils
import com.league.zjxn.app.SPSConstance
import com.league.zjxn.base.BaseManager
import com.league.zjxn.entity.*
import com.league.zjxn.utils.JsonUtils
import com.module.lemlin.http.CustomException
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.msg
import rxhttp.RxHttp
import rxhttp.await
import kotlin.Exception

open class AccessRepository : BaseManager() {

    suspend fun userSendSmsCode(mobile: String, scene: String): SendSmsCodeResponse =
        RxHttp.postForm("/api/sendSmsCode")
            .add("scene", scene)
            .add("mobile", mobile)
            .await()

    suspend fun userLoginPass(maps: MutableMap<String, String>): LoginResponse {
        val responseData = RxHttp.postForm("/api/user/login")
            .addAll(maps)
            .await<ResponseData>()
        if (responseData.code == 2000) {
            val userInfo = try {
                JsonUtils.objectToClass(responseData.data, Login::class.java)
            } catch (e: Exception) {
                throw Exception()
            }
            return LoginResponse(data = userInfo).apply {
                msg = responseData.msg
                code = responseData.code
            }
        }
        throw CustomException(errorCode = responseData.code.toString(), message = responseData.msg)
    }

    suspend fun userLoginSms(maps: MutableMap<String, String>): LoginResponse {
        val responseData = RxHttp.postForm("/api/user/SMSLogin")
            .addAll(maps)
            .await<ResponseData>()
        if (responseData.code == 2000) {
            val userInfo = try {
                JsonUtils.objectToClass(responseData.data, Login::class.java)
            } catch (e: Exception) {
                throw Exception()
            }
            return LoginResponse(data = userInfo).apply {
                msg = responseData.msg
                code = responseData.code
            }
        }
        throw CustomException(errorCode = responseData.code.toString(), message = responseData.msg)
    }


    suspend fun userUpdatePass(maps: MutableMap<String, String>): HttpResponseData =
        RxHttp.postForm("/api/user/forgetPassword")
            .addAll(maps)
            .await()

    suspend fun userRegister(
        maps: MutableMap<String, String>
    ): HttpResponseData = RxHttp.postForm("/api/user/register")
        .addAll(maps)
        .await()


    suspend fun userSettingPwd(maps: MutableMap<String, String>): LoginResponse {
        val responseData = RxHttp.postForm("/api/user/setNewPassword")
            .addHeader("token", SPSConstance.token ?: "")
            .addAll(maps)
            .await<ResponseData>()
        if (responseData.code == 2000) {
            val userInfo = try {
                JsonUtils.objectToClass(responseData.data, Login::class.java)
            } catch (e: Exception) {
                throw Exception()
            }
            return LoginResponse(data = userInfo).apply {
                msg = responseData.msg
                code = responseData.code
            }
        }
        throw CustomException(errorCode = responseData.code.toString(), message = responseData.msg)
    }

    suspend fun userSettingPhone(maps: MutableMap<String, String>): LoginResponse {
        val responseData = RxHttp.postForm("/api/user/changeAccount")
            .addHeader("token", SPSConstance.token ?: "")
            .addAll(maps)
            .await<ResponseData>()
        if (responseData.code == 2000) {
            val userInfo = try {
                JsonUtils.objectToClass(responseData.data, Login::class.java)
            } catch (e: Exception) {
                throw Exception()
            }
            return LoginResponse(data = userInfo).apply {
                msg = responseData.msg
                code = responseData.code
            }
        }
        throw CustomException(errorCode = responseData.code.toString(), message = responseData.msg)
    }
}